package org.gary.admin.dao.impl;

import java.sql.SQLException;
import javax.annotation.Resource;

import org.gary.admin.dao.LoggerDao;
import org.gary.admin.model.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

/**
 * 
 * @author lengreen
 *
 */
@Component("loggerDao")
public class LoggerDaoImpl implements LoggerDao {
	private HibernateTemplate hibernateTemplate;
	private String hql;

	@Override
	public void delete(String from, String to) {
		setHql("delete from " + Logger.class.getName()
				+ " entity where entity.createDate between '" + from
				+ " 06:01:01' and '" + to + " 06:01:01'");
		hibernateTemplate.execute(new HibernateCallback<Logger>() {

			public Logger doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery(getHql());
				q.executeUpdate();
				return null;
			}
		});
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public String getHql() {
		return hql;
	}
}
